package com.hdit.guimei.web;


import com.hdit.guimei.domain.Goods;
import com.hdit.guimei.domain.Seckill;
import com.hdit.guimei.service.impl.SeckillServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServlet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 张宝玉
 * @date 2021/1/20 14:31
 */
@Controller
public class SeckillController extends HttpServlet {
    @Autowired
    public SeckillServiceImpl seckillService;
    //查找秒杀时间段得所有商品
    @RequestMapping("/findgoodsinseckill")
    public String findgoodsinseckill(String start_time, String end_time, Model model){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date time_start = sdf.parse(start_time);
            Date time_end=sdf.parse(end_time);
            List<Goods> goodsList = seckillService.findseckillgoods(time_start, time_end);
            if (!goodsList.isEmpty()){
                model.addAttribute("goodlist",goodsList);
                return "";
            }
            else {
                return "";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "404";
    }
    //添加秒杀时间段商品
    @RequestMapping("/addgoodseckill")
    public String addgoodseckill(@RequestBody Seckill seckill){
        int i = seckillService.addgoodinseckill(seckill);
        if (i==1){
            return "";
        }else {
            return  "";
        }
    }
    @RequestMapping("/delegoodseckill")
    public String delegoodseckill(int good_id){
        int i = seckillService.delegoodseckill(good_id);
        if (i==1){
            return "";
        }else{
            return  "";
        }
    }
}
